package com.service.impl;

import com.dto.LoginDto;
import com.mapper.LoginMapper;
import com.pojo.User;
import com.service.LoginService;
import com.utils.Md5Utils;
import com.utils.SnowFlake;
import com.utils.Static;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private SnowFlake snowFlake;

    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @Override
    public User login(LoginDto loginDto) {
        String s = Md5Utils.MD5Encode(loginDto.getPassword(), "utf-8", false);
        loginDto.setPassword(s);
        User user=loginMapper.login(loginDto);
        return user;
    }

    /**
     * 用户注册
     * @param loginDto
     */
    @Override
    public void register(LoginDto loginDto) {
        loginDto.setId(snowFlake.nextId());
        String s = Md5Utils.MD5Encode(loginDto.getPassword(), "utf-8", false);
        loginDto.setPassword(s);
        User user = new User();
        BeanUtils.copyProperties(loginDto,user);
        user.setName(Static.NAME);
        user.setHead(Static.HEAD);
        loginMapper.register(user);
    }
}
